<?php

namespace addons\ad\controllers;
use addons\ad\models\AdTypeModel;
use controllers\AdminApi;

class type extends AdminApi {
	/**
	 * 初始化
	 */
	public function onStepUp(){
		$this->model = new AdTypeModel();
	}

	/**
	 * 分类接口
	 * @param string $title
	 * @return array
	 */
	public function index($title = '') {
		$map = [];
		if ($title){
			$map['a.title like'] = $title;
		}
		$limitSize = $this->model::Types;
		$list = $this->model->alias('a')->field('a.*')->where($map)->select();
		$count = $this->model->alias('a')->count($map);
		foreach ($list as $key => &$val){
			$val['limit_size_format'] = $limitSize[$val['limit_size']];
		}
		return self::returnJson($list , $count);
	}

	/**
	 * 更新前操作
	 * @param array $data
	 * @return bool
	 */
	protected function onUpdateBefore(&$data = []){
		if (self::$post['id']) {
			$find = $this->model->getFind(self::$post['id']);
			if (!$find) {
				$this->error = '广告类型不存在';
				return false;
			}
		}
		if (self::$post['limit_size'] != 1){
			self::$post['limit_policy'] = '0';
		}
		return true;
	}

	/**
	 * 编辑-查询资料时
	 * @return array
	 * @author Colin <amcolin@126.com>
	 * @date 2021-12-30 下午3:13
	 */
	public function onFind(){
		$find = $this->getFind();
		$select = 0;
		if ($find){
			$select = $find['limit_size'];
		}
		$types = $this->model::Types;
		return $this->getSelectOptions($types , $select);
	}

	/**
	 * 删除后
	 * @param bool $status
	 * @param array $find
	 * @return bool
	 */
	protected function onDeleteAfter($status, $find = []){
		if ($find['image']){
			unlink(ltrim($find['image'] , '/'));
		}
		return true;
	}

	/**
	 * 面包屑
	 * @return array|string[]
	 * @author Colin <amcolin@126.com>
	 * @date 2021-12-30 下午3:22
	 */
	protected static function setCrumbs() {
		return ['广告管理' , '广告类型'];
	}

	/**
	 * 页面标题
	 * @return string
	 * @author Colin <amcolin@126.com>
	 * @date 2021-12-30 下午3:46
	 */
	protected static function setPageTitle() {
		return '广告类型';
	}
}
